package com.kingdee.eas.xr.helper;

/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.


import com.kingdee.bos.ctrl.kdf.table.ICell;
import com.kingdee.bos.ctrl.kdf.table.IColumn;
import com.kingdee.bos.ctrl.kdf.table.IRow;
import com.kingdee.bos.ctrl.kdf.table.KDTDefaultCellEditor;
import com.kingdee.bos.ctrl.kdf.table.KDTIndexColumn;
import com.kingdee.bos.ctrl.kdf.table.KDTSortManager;
import com.kingdee.bos.ctrl.kdf.table.KDTable;
import com.kingdee.bos.ctrl.kdf.table.event.KDTEditAdapter;
import com.kingdee.bos.ctrl.kdf.table.event.KDTEditListener;
import com.kingdee.bos.ctrl.kdf.table.foot.KDTFootManager;
import com.kingdee.bos.ctrl.kdf.util.editor.ICellEditor;
import com.kingdee.bos.ctrl.kdf.util.style.StyleAttributes;
import com.kingdee.bos.ctrl.kdf.util.style.Styles;
import com.kingdee.bos.ctrl.swing.IKDEditor;
import com.kingdee.bos.ctrl.swing.KDDatePicker;
import com.kingdee.bos.ctrl.swing.KDFormattedTextField;
import com.kingdee.bos.ctrl.swing.KDTree;
import com.kingdee.bos.dao.AbstractObjectValue;
import com.kingdee.eas.util.client.EASResource;
import java.awt.Color;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.swing.event.EventListenerList;

// Referenced classes of package com.kingdee.eas.ec.common.client:
//            EcBaseRender, EcEditorUtil

public class TableHelper 
{
    public class KDTSortManagerEx extends KDTSortManager
    {

        public void sort(int colIndex, int sortType)
        {
            super.sort(colIndex, sortType);
        }

        final TableHelper this$0;

        public KDTSortManagerEx(KDTable arg0)
        {
            this$0 = TableHelper.this;
        }
    }


    public TableHelper()
    {
    }

    public static void setColDisplayFormatNumber(KDTable tbl, String cols[])
    {
        int length = cols.length;
        for(int i = 0; i < length; i++)
            if(tbl.getColumn(cols[i]) != null)
            {
                IColumn col = tbl.getColumn(cols[i]);
//                col.setRenderer(new EcBaseRender("$number$"));
            }

    }

    public static void SetTableDateFormat(KDTable tbl, String cols[])
    {
        int length = cols.length;
        for(int i = 0; i < length; i++)
            if(tbl.getColumn(cols[i]) != null)
            {
                IColumn col = tbl.getColumn(cols[i]);
                KDDatePicker bizDate = new KDDatePicker();
                bizDate.setSupportedEmpty(false);
                col.setEditor(new KDTDefaultCellEditor(bizDate));
                String formatString = "%{yyyy-MM-dd}t";
                col.getStyleAttributes().setNumberFormat(formatString);
            }

    }

    public static IColumn getColumn(KDTable table, String fieldName)
    {
        IColumn column = null;
        if(table != null && fieldName != null && fieldName.trim().length() != 0)
            column = table.getColumn(fieldName);
        return column;
    }

    public static void setWidthForColumn(KDTable tbl, Map map)
    {
        Set keySet = map.keySet();
        Iterator iter = keySet.iterator();
        String strCol = null;
        Integer length = null;
        Object obj = null;
        do
        {
            if(!iter.hasNext())
                break;
            strCol = (String)iter.next();
            IColumn col = getColumn(tbl, strCol);
            if(col != null)
            {
                obj = map.get(strCol);
                length = (Integer)obj;
                col.setWidth(length.intValue());
            }
        } while(true);
    }

    public static void setFieldsHorizontalAlignment(com.kingdee.bos.ctrl.kdf.util.style.Styles.HorizontalAlignment hAlign, KDTable table, String fieldNames[])
    {
        if(fieldNames != null)
        {
            IColumn col = null;
            int i = 0;
            for(int size = fieldNames.length; i < size; i++)
            {
                col = getColumn(table, fieldNames[i]);
                if(col != null)
                    col.getStyleAttributes().setHorizontalAlign(hAlign);
            }

        }
    }

    public static void setRate(KDTable table, String fieldNames[], int precision)
    {
        if(fieldNames != null)
        {
            IColumn col = null;
            for(int i = 0; i < fieldNames.length; i++)
            {
                col = getColumn(table, fieldNames[i]);
                if(col != null)
                {
                    col.setEditor(new KDTDefaultCellEditor(getBigDecimalTextField(precision, EcConstant.ONE_HUNDRED, EcConstant.ZERO)));
                    col.getStyleAttributes().setNumberFormat(getFormatString(precision));
                }
            }

        }
    }

    public static void setNotNegative(KDTable table, String fieldNames[], int precision)
    {
        if(fieldNames != null)
        {
            IColumn col = null;
            for(int i = 0; i < fieldNames.length; i++)
            {
                col = getColumn(table, fieldNames[i]);
                if(col != null)
                {
                    col.setEditor(new KDTDefaultCellEditor(getBigDecimalTextField(precision, EcConstant.MAX_VALUE, EcConstant.ZERO)));
                    col.getStyleAttributes().setNumberFormat(getFormatString(precision));
                }
            }

        }
    }

    public static void setNegative(KDTable table, String fieldNames[], int precision)
    {
        if(fieldNames != null)
        {
            IColumn col = null;
            for(int i = 0; i < fieldNames.length; i++)
            {
                col = getColumn(table, fieldNames[i]);
                if(col != null)
                {
                    col.setEditor(new KDTDefaultCellEditor(getBigDecimalTextField(precision, EcConstant.MAX_VALUE, EcConstant.MIN_VALUE)));
                    col.getStyleAttributes().setNumberFormat(getFormatString(precision));
                }
            }

        }
    }

    public static void rowToUseObject(IRow row, String cols[], String propertys[])
    {
        if(cols == null || row == null || propertys == null)
            return;
        Object obj = row.getUserObject();
        if(obj == null)
            return;
        if(obj instanceof AbstractObjectValue)
        {
            AbstractObjectValue objectValue = (AbstractObjectValue)obj;
            Enumeration key = null;
            int length = cols.length > propertys.length ? propertys.length : cols.length;
            String strCol = null;
            String proCol = null;
            Object colValue = null;
            for(int i = 0; i < length; i++)
            {
                strCol = cols[i];
                proCol = propertys[i];
                key = objectValue.keys();
                boolean flag = checkContain(proCol, key);
                flag = true;
                ICell cell = row.getCell(strCol);
                if(flag && cell != null)
                {
                    colValue = row.getCell(strCol).getValue();
                    objectValue.put(proCol, colValue);
                }
            }

        }
    }

    public static void rowToUseObject(IRow row, String cols[])
    {
        if(cols == null || row == null)
            return;
        Object obj = row.getUserObject();
        if(obj == null)
            return;
        if(obj instanceof AbstractObjectValue)
        {
            AbstractObjectValue objectValue = (AbstractObjectValue)obj;
            int length = cols.length;
            String strCol = null;
            Object colValue = null;
            for(int i = 0; i < length; i++)
            {
                strCol = cols[i];
                boolean flag = true;
                ICell cell = row.getCell(strCol);
                if(flag && cell != null)
                {
                    colValue = row.getCell(strCol).getValue();
                    objectValue.put(strCol, colValue);
                }
            }

        }
    }

    public static void useObjectToRow(AbstractObjectValue useObject, IRow row, String cols[])
    {
        if(cols == null || row == null || useObject == null)
            return;
        row.setUserObject(useObject);
        Enumeration key = null;
        int length = cols.length;
        String strCol = null;
        Object colValue = null;
        for(int i = 0; i < length; i++)
        {
            strCol = cols[i];
            key = useObject.keys();
            boolean flag = checkContain(strCol, key);
            ICell cell = row.getCell(strCol);
            if(flag && cell != null)
            {
                colValue = useObject.get(strCol);
                row.getCell(strCol).setValue(colValue);
            }
        }

    }

    public static void useObjectToRow(AbstractObjectValue useObject, IRow row, String cols[], String propertys[])
    {
        if(cols == null || row == null || useObject == null)
            return;
        row.setUserObject(useObject);
        Enumeration key = null;
        int length = cols.length > propertys.length ? propertys.length : cols.length;
        String strCol = null;
        String property = null;
        Object colValue = null;
        for(int i = 0; i < length; i++)
        {
            strCol = cols[i];
            property = propertys[i];
            key = useObject.keys();
            boolean flag = checkContain(property, key);
            ICell cell = row.getCell(strCol);
            if(flag && cell != null)
            {
                colValue = useObject.get(property);
                row.getCell(strCol).setValue(colValue);
            }
        }

    }

    public static boolean checkContain(String property, Enumeration keys)
    {
        Object obj = null;
        boolean flag = false;
        do
        {
            if(!keys.hasMoreElements())
                break;
            obj = keys.nextElement();
            if(!obj.toString().equalsIgnoreCase(property))
                continue;
            flag = true;
            break;
        } while(true);
        return flag;
    }

    public static void setLockFields(KDTable table, String fieldNames[], boolean isLock)
    {
        if(fieldNames != null)
        {
            IColumn column = null;
            int i = 0;
            for(int size = fieldNames.length; i < size; i++)
            {
                column = getColumn(table, fieldNames[i]);
                if(column != null)
                    column.getStyleAttributes().setLocked(isLock);
            }

        }
    }

    public static void setBigDecimalFieldsPrecision(int precision, KDTable table, String fieldNames[])
    {
        if(fieldNames != null)
        {
            int i = 0;
            for(int size = fieldNames.length; i < size; i++)
                setBigDecimalFieldsPrecisionByColumn(getColumn(table, fieldNames[i]), precision);

        }
    }

    public static void setBigDecimalFieldsPrecisionByColumn(IColumn column, int precision)
    {
        if(column != null)
        {
            column.setEditor(new KDTDefaultCellEditor(getBigDecimalTextField(precision, null)));
            column.getStyleAttributes().setNumberFormat(getFormatString(precision));
        }
    }

    public static KDFormattedTextField getBigDecimalTextField(int precision, BigDecimal maxValue)
    {
        KDFormattedTextField colTxtField = new KDFormattedTextField();
        colTxtField.setDataType(1);
        colTxtField.setPrecision(precision);
        colTxtField.setHorizontalAlignment(4);
        colTxtField.setSupportedEmpty(true);
        colTxtField.setMaximumValue(maxValue);
        return colTxtField;
    }

    public static KDFormattedTextField getBigDecimalTextField(int precision, BigDecimal maxValue, BigDecimal minValue)
    {
        KDFormattedTextField colTxtField = new KDFormattedTextField();
        colTxtField.setDataType(1);
        colTxtField.setPrecision(precision);
        colTxtField.setHorizontalAlignment(4);
        colTxtField.setSupportedEmpty(true);
        colTxtField.setMaximumValue(maxValue);
        colTxtField.setMinimumValue(minValue);
        return colTxtField;
    }

    public static void setRequiredFields(KDTable table, String fieldNames[], boolean isRequired)
    {
        if(fieldNames != null)
        {
            IColumn column = null;
            int i = 0;
            for(int size = fieldNames.length; i < size; i++)
            {
                column = getColumn(table, fieldNames[i]);
                if(column != null)
                    column.setRequired(isRequired);
            }

        }
    }

    public static void setHideFields(KDTable table, String fieldNames[], boolean isHide)
    {
        if(fieldNames != null)
        {
            IColumn column = null;
            int i = 0;
            for(int size = fieldNames.length; i < size; i++)
            {
                column = getColumn(table, fieldNames[i]);
                if(column != null)
                    column.getStyleAttributes().setHided(isHide);
            }

        }
    }

    public static void setColEditor(KDTable table, Map columnEditors)
    {
        if(table != null && columnEditors != null)
        {
            Iterator itor = columnEditors.keySet().iterator();
            Object key = null;
            Object editor = null;
            IColumn column = null;
            do
            {
                if(!itor.hasNext())
                    break;
                key = itor.next();
                if(key instanceof String)
                    column = getColumn(table, key.toString());
                else
                if(key instanceof Integer)
                    column = getColumn(table, Integer.parseInt(key.toString()));
                editor = columnEditors.get(key);
                if(column != null && (editor instanceof IKDEditor))
                    column.setEditor(new KDTDefaultCellEditor((IKDEditor)editor));
                else
                if(column != null && (editor instanceof KDTDefaultCellEditor))
                    column.setEditor((ICellEditor)editor);
            } while(true);
        }
    }

    public static IColumn getColumn(KDTable table, int columnIndex)
    {
        IColumn column = null;
        if(table != null && columnIndex > -1)
            column = table.getColumn(columnIndex);
        return column;
    }

    public static Object getFieldValue(IRow row, String fieldName)
    {
        Object result = null;
        ICell cell = getCell(row, fieldName);
        if(cell != null)
            result = cell.getValue();
        return result;
    }

    public static Object getFieldValue(KDTable table, int rowIndex, String fieldName)
    {
        Object result = null;
        ICell cell = getCell(table, rowIndex, fieldName);
        if(cell != null)
            result = cell.getValue();
        return result;
    }

    public static String getFieldName(KDTable table, int columnIndex)
    {
        String fieldName = null;
        if(table != null && columnIndex > -1)
            fieldName = table.getColumnKey(columnIndex);
        return fieldName;
    }

    public static Object getFieldValue(KDTable table, int rowIndex, int columnIndex)
    {
        Object result = null;
        ICell cell = getCell(table, rowIndex, columnIndex);
        if(cell != null)
            result = cell.getValue();
        return result;
    }

    public static java.util.List getFieldValues(KDTable table, int rowIndex, String fieldNames[])
    {
        java.util.List values = new ArrayList();
        if(table != null && rowIndex > -1 && fieldNames != null)
        {
            int size = fieldNames.length;
            for(int i = 0; i < size; i++)
                values.add(i, getFieldValue(table, rowIndex, fieldNames[i]));

        }
        return values;
    }

    public static java.util.List getMapFieldValues(KDTable table, String fieldNames[])
    {
        java.util.List allList = new ArrayList();
        if(table != null && fieldNames != null)
        {
            for(int i = 0; i < table.getRowCount(); i++)
            {
                Map map = getMapFieldValues(table, i, fieldNames);
                allList.add(map);
            }

        }
        return allList;
    }

    public static Map getMapFieldValues(KDTable table, int rowIndex, String fieldNames[])
    {
        Map map = new HashMap();
        java.util.List list = getFieldValues(table, rowIndex, fieldNames);
        for(int j = 0; j < fieldNames.length; j++)
            map.put(fieldNames[j], list.get(j));

        return map;
    }

    public static java.util.List getFieldValues(KDTable table, int rowIndex, int columnIndexes[])
    {
        java.util.List values = new ArrayList();
        if(table != null && rowIndex > -1 && columnIndexes != null)
        {
            int size = columnIndexes.length;
            for(int i = 0; i < size; i++)
                values.add(i, getFieldValue(table, rowIndex, columnIndexes[i]));

        }
        return values;
    }

    public static void setFieldValues(KDTable table, int rowIndex, String fieldNames[], Object values[])
    {
        if(fieldNames != null)
        {
            int vSize = values.length;
            int i = 0;
            for(int size = fieldNames.length; i < size; i++)
                if(i > -1 && i < vSize)
                    setFieldValue(table, rowIndex, fieldNames[i], values[i]);
                else
                    setFieldValue(table, rowIndex, fieldNames[i], null);

        }
    }

    public static void setFieldValues(KDTable table, int rowIndex, int columnIndexes[], Object values[])
    {
        if(columnIndexes != null)
        {
            int vSize = values.length;
            int i = 0;
            for(int size = columnIndexes.length; i < size; i++)
                if(i > -1 && i < vSize)
                    setFieldValue(table, rowIndex, columnIndexes[i], values[i]);
                else
                    setFieldValue(table, rowIndex, columnIndexes[i], null);

        }
    }

    public static void setFieldValue(KDTable table, int rowIndex, String fieldName, Object value)
    {
        ICell cell = getCell(table, rowIndex, fieldName);
        if(cell != null)
            cell.setValue(value);
    }

    public static void setFieldValue(IRow row, String fieldName, Object value)
    {
        ICell cell = row.getCell(fieldName);
        if(cell != null)
            cell.setValue(value);
    }

    public static void setFieldValue(KDTable table, int rowIndex, int columnIndex, Object value)
    {
        ICell cell = getCell(table, rowIndex, columnIndex);
        if(cell != null)
            cell.setValue(value);
    }

    public static ICell getCell(KDTable table, int rowIndex, String fieldName)
    {
        ICell cell = null;
        if(table != null && rowIndex > -1 && fieldName != null && fieldName.trim().length() != 0)
            cell = table.getRow(rowIndex).getCell(fieldName);
        return cell;
    }

    public static ICell getCell(IRow row, String fieldName)
    {
        ICell cell = null;
        if(fieldName != null && fieldName.trim().length() != 0)
            cell = row.getCell(fieldName);
        return cell;
    }

    public static ICell getCell(KDTable table, int rowIndex, int columnIndex)
    {
        ICell cell = null;
        if(table != null && rowIndex > -1 && columnIndex > -1)
            cell = table.getRow(rowIndex).getCell(columnIndex);
        return cell;
    }

    public static String getFormatString(int precision)
    {
        if(precision == 0)
            return "%R-{#,##0}f";
        StringBuffer buffer = new StringBuffer();
        if(precision == 0)
            buffer.append("#");
        else
            buffer.append("0.");
        for(int i = 0; i < precision; i++)
            buffer = buffer.append("0");

        StringBuffer formatString = new StringBuffer();
        formatString.append("%r-[=]{#,##").append(buffer).append("}f");
        return formatString.toString();
    }






    public static String[] getStringsTableColumns(KDTable tbl)
    {
        if(tbl == null)
            return null;
        int count = tbl.getColumnCount();
        if(count == 0)
            return null;
        String str[] = new String[count];
        for(int i = 0; i < count; i++)
            str[i] = tbl.getColumnKey(i);

        return str;
    }

    public static java.util.List getListTableColumns(KDTable tbl)
    {
        if(tbl == null)
            return null;
        int count = tbl.getColumnCount();
        if(count == 0)
            return null;
        java.util.List list = new ArrayList();
        for(int i = 0; i < count; i++)
            list.add(tbl.getColumnKey(i));

        return list;
    }

    public static java.util.List getRequiredColumn(KDTable tbl, String propertyNames[])
    {
        java.util.List list = new ArrayList();
        if(tbl == null)
            return list;
        if(propertyNames == null)
        {
            int count = tbl.getColumnCount();
            IColumn col = null;
            for(int i = 0; i < count; i++)
            {
                col = tbl.getColumn(i);
                if(col.isRequired())
                    list.add(col.getKey());
            }

        } else
        {
            list = Arrays.asList(propertyNames);
        }
        return list;
    }

    public static String getHeadCellName(IRow row, String colName)
    {
        String headCellName = "";
        ICell cell = row.getCell(colName);
        if(cell == null || cell.getValue() == null)
        {
            return "";
        } else
        {
            headCellName = (String)cell.getValue();
            return headCellName;
        }
    }

    public static boolean treeViewIsEmpty(KDTree treeWbs)
    {
        return treeWbs.getModel() == null;
    }

    public static boolean isEmpty(String str)
    {
        return str == null || str.trim().length() == 0;
    }

    public static boolean isEmpty(Object obj)
    {
        if(obj == null)
            return true;
        if(obj instanceof BigDecimal)
            return ((BigDecimal)obj).compareTo(EcConstant.ZERO) == 0;
        else
            return isEmpty(obj.toString());
    }

    public void tableSort(KDTable tbl, String sortColName)
    {
        int sortCol = tbl.getColumnIndex(sortColName);
        if(sortCol < 0)
        {
            return;
        } else
        {
            KDTSortManagerEx kdtsm = new KDTSortManagerEx(tbl);
            kdtsm.sort(1, sortCol);
            tbl.setRefresh(true);
            return;
        }
    }

    public static void tableSort(KDTable tbl)
    {
        for(int i = 0; i < tbl.getColumnCount(); i++)
            tbl.getColumn(i).setSortable(true);

        KDTSortManager sortManager = new KDTSortManager(tbl);
        sortManager.setClickCount(1);
        sortManager.setSortAuto(true);
    }
}


/*
	DECOMPILATION REPORT

	Decompiled from: F:\qs\lib\client\industry\ec_common-client.jar
	Total time: 31 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/